var token = selectByKey("like_token");
var dingDan = null;
function reEnter () {
    document.querySelector('.info').style.display = 'block';
    setTimeout(function() {
        window.location.href = '../personal/enter.html';
    },1500);
}
//购物清单模板
function shopHtml(data, scriptdom){
    var mainmax = document.getElementById('mainlist');
    var html = '';
    for (var k = 0; k < data.length; k++) {
        var htmlm = scriptdom.innerHTML;
        htmlm = htmlm.replace(/\{\{src\}\}/, data[k].good_img);
        htmlm = htmlm.replace(/\{\{name\}\}/, data[k].good_name);
        htmlm = htmlm.replace(/\{\{num\}\}/g, data[k].good_num);
        htmlm = htmlm.replace(/\{\{price\}\}/, data[k].good_price);
        html += htmlm;
    }
    mainmax.innerHTML = html;
}
//订单页跳过来用的模板
function orderHtml(data, scriptdom){
    var mainmax = document.getElementById('mainlist');
    var html = '';
    for (var k = 0; k < data.length; k++) {
        var htmlm = scriptdom.innerHTML;
        htmlm = htmlm.replace(/\{\{src\}\}/, data[k].good_img);
        htmlm = htmlm.replace(/\{\{name\}\}/, data[k].good_name);
        htmlm = htmlm.replace(/\{\{num\}\}/g, data[k].good_num);
        htmlm = htmlm.replace(/\{\{price\}\}/, data[k].good_price);
        html += htmlm;
    }
    mainmax.innerHTML = html;
}
//支付模块
function pay(){
    var foot = document.querySelector("footer");
    Tap(foot,function(){
        console.log(dingDan.innerHTML)
        $.ajax({
            url:pathName+"/api/v1/pay",
            type:"post",
            dataType:"json",
            headers:{
                "Accept":"application/json",
                "Authorization":"Bearer "+token,
            },
            data:{
                "out_trade_no":dingDan.innerHTML
            },
            success:function(data){
                console.log(data);
                localStorage.clear();
                window.location.href = data.success;
            },
            error:function () {
                if(!token){
                    reEnter();
                }
            }
        })
    });
}
window.onload = function(){
    var listMode = document.getElementById("listMod");
    var yunfei = document.getElementById("yunfei");
    var zongjia = document.getElementById("zongjia");
    var finalMoney = document.getElementById("finalMoney");
    var totleNum = document.getElementById("totleNum");
    var packingFee = document.getElementById("packingFee");
    var totleMoney = document.getElementById("totlaMoney");
    dingDan = document.querySelector('.p2 i');
    var addressInfo = document.querySelector('.p3 i');
    var orderId = localStorage.getItem("orderId");
    var couponPrice = document.getElementById("couponPrice");
    var adressc = document.getElementById("adressc");
    var mask = document.querySelector(".mask");
    var payOrder = location.search.substr(4);  //从订单详情也跳过来的id
    if(payOrder){
        //从我的订单中过来
        requestMsg(payOrder);
    }else{
        //正常支付过程
        requestMsg(orderId);
    }

    function requestMsg (order) {
        $.ajax({
            url:pathName+"/api/v1/member/cart/122/order",
            type:"GET",
            headers:{
                "Accept":"application/json",
                "Authorization":"Bearer "+ token
            },
            data:{
                "order_id":order
            },

            success:function(data){
                mask.style.display = "none";
                data = data.order;
                if(data.remark){
                    var leaveMsg = document.createElement("p");
                    adressc.appendChild(leaveMsg);
                    leaveMsg.innerHTML = "买家留言:" + data.remark;
                }
                addressInfo.innerHTML = data.address+"/"+data.name+"/"+data.phone;
                shopHtml(data.goodDetail, listMode);
                yunfei.innerHTML = data.trans_cost;
                if(data.type == 1){
                    packingFee.innerHTML = 0;
                }else{
                    packingFee.innerHTML = data.packingFee + data.shortFreight;
                }
                if(data.coupon_price){
                    couponPrice.innerHTML = "-" + data.coupon_price;
                }else{
                    couponPrice.innerHTML = 0;
                }
                zongjia.innerHTML = (parseFloat(data.pay_total) + parseFloat(data.coupon_price)).toFixed(2);
                totleMoney.innerHTML = data.pay_total;
                var residualNum = data.memberBalance || 0;
                finalMoney.innerHTML = (data.pay_total - residualNum).toFixed(2);
                var tempArr = data.goodDetail;
                dingDan.innerHTML = data.order_id;
                var sum = 0;
                for(var i = 0; i < tempArr.length; i++){
                    sum += parseInt(tempArr[i].good_num);
                }
                totleNum.innerHTML = sum;
                pay();
            },
            error:function() {
                if(!token){
                    reEnter();
                }
            }
        });
    }


}